﻿using BusinessRepository;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using BusinessEntity;
using BusinessEntity.CGGLQuerry;
using BusinessEntity.CGGL;
using BusinessRule.Organization;
using APICommond;
using learun.util;

namespace BusinessRule
{
    public enum TableName
    {
        采购计划,
        采购项目,
        采购标段,
        采购招标,
        评标中标,
        采购合同,
        采购合同变更,
        采购竣工
    }

    public class CGGLAudit
    {
        //待提交  未审核   审核中    作废    已审核      驳回
        //DRAFT  UNCHECK, CHECKING,INVALID,  CHECKED    REJECT
        public static string GetXMCGSPZT(string opinion)
        {
            //CHECKED
            //UNCHECK
            //CHECKING
            //RESTART
            //INVALID
            //REJECT

            //string ztz = "0";
            //if (opinion == "UNCHECK")//未审核
            //{
            //    ztz = "0";
            //}
            //else if (opinion == "DRAFT")//待提交
            //{
            //    ztz = "-1";
            //}
            //else if (opinion == "CHECKING")//审核中
            //{
            //    ztz = "1";
            //}
            //else if (opinion == "INVALID")//作废
            //{
            //    ztz = "4";
            //}
            //else if (opinion == "CHECKED")//已审核
            //{
            //    ztz = "2";
            //}
            //else if (opinion == "REJECT")//REJECT
            //{
            //    ztz = "3";
            //}
            //else if (opinion == "HANDEL")//202109 作废 待提交
            //{
            //    ztz = "-1";
            //}


            string ztz = "0";
            if (opinion == "DRAFT")//待提交
            {
                ztz = "0";
            }
            else if (opinion == "UNCHECK")//未审核
            {
                ztz = "1";
            }
            else if (opinion == "CHECKING")//审核中
            {
                ztz = "2";
            }
            else if (opinion == "INVALID")//作废
            {
                ztz = "5";
            }
            else if (opinion == "CHECKED")//已审核
            {
                ztz = "3";
            }
            else if (opinion == "REJECT")//REJECT
            {
                ztz = "4";
            }
            else if (opinion == "HANDEL")//202109 作废 待提交
            {
                ztz = "0";
            }

            return ztz;
        }

        public static string GetXMCGSPZTName(string opinion)
        {
            //CHECKED
            //UNCHECK
            //CHECKING
            //RESTART
            //INVALID
            //REJECT

            //string ztz = "0";
            //if (opinion == "UNCHECK")//未审核
            //{
            //    ztz = "0";
            //}
            //else if (opinion == "DRAFT")//待提交
            //{
            //    ztz = "-1";
            //}
            //else if (opinion == "CHECKING")//审核中
            //{
            //    ztz = "1";
            //}
            //else if (opinion == "INVALID")//作废
            //{
            //    ztz = "4";
            //}
            //else if (opinion == "CHECKED")//已审核
            //{
            //    ztz = "2";
            //}
            //else if (opinion == "REJECT")//REJECT
            //{
            //    ztz = "3";
            //}
            //else if (opinion == "HANDEL")//202109 作废 待提交
            //{
            //    ztz = "-1";
            //}


            string ztz = "";
            if (opinion == "0")//待提交
            {
                ztz = "DRAFT";
            }
            else if (opinion == "1")//未审核
            {
                ztz = "UNCHECK";
            }
            else if (opinion == "2")//审核中
            {
                ztz = "CHECKING";
            }
            else if (opinion == "5")//作废
            {
                ztz = "INVALID";
            }
            else if (opinion == "3")//已审核
            {
                ztz = "CHECKED";
            }
            else if (opinion == "4")//REJECT
            {
                ztz = "REJECT";
            }
            else if (opinion == "0")//202109 作废 待提交
            {
                ztz = "HANDEL";
            }

            return ztz.ToLower();
        }

        public static Dictionary<string, string> listTableName = new Dictionary<string, string>()
        {
            ["采购计划"] = "CG_CGJH",
            ["采购项目"] = "CG_CGXMML",
            ["采购标段"] = "CG_BDXXML",
            ["采购招标"] = "CG_ZBXXML",
            ["评标中标"] = "CG_PBZBML",

            ["采购合同"] = "CG_HTXX",
            //["采购合同变更"] = "CG_XMYSML",
            ["采购竣工"] = "CG_XMYSML"
        };

       

        //TableName name
        public static async Task<bool> SetStatus(string ywid, string name, string status, string uname, string userid, decimal sdje, string additionInfo)
        {
            try
            {

                SPLISTUSERCONT splog = new SPLISTUSERCONT();
                splog.id = CommonHelper.GetUniqueId().ToString();
                splog.cid = ywid;
                splog.scid = "CG" + name;
                splog.userid = userid;
                splog.ispost = status;
                splog.createdate = DateTime.Now;
                splog.createuser = uname;

                return await TranstionRule.GetRunActionTransAsync(async irp =>
                {
                    string strsql = "update " + listTableName[name] + " set zt = '" + status + "' where listid = '" + ywid + "'";
                    await irp.ExecuteSql(strsql);

                    //添加审批记录
                    await irp.Insert(splog);

 
                    return true;

                });


                //await new RepositoryFactory().BaseRepository().ExecuteSql(strsql);
                //return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

    }
}
